// 查   find   findOne
// 增   insert({},[])   insertOne  insertMany save()
// 改   update updateOne updateMany
// 删除   deleteOne deleteMany remove


// insert({},[])   insertOne  insertMany save()
db.users.save({username:"hello"})
db.users.save({username:"huahua"})
db.users.save([{username:"xiaoming"},{username:"靓仔"}])

db.users.insertOne({
    username:"kiki"
})


db.users.insertOne({
    username:"kity"
})

db.users.insert({
    username:"happy"
})

db.users.insert([{
    username:"UZI"
},{
    username:"THeShy"
}])

db.users.insertMany([{
    username:"Faker"
},{
    username:"厂长"
},{
    username:"弥勒"
}])


db.users.insert({
    username:'hoho'
})

db.users.insert([{
    username:'yuyu'
},
{
    username:"bibi"
}])



db.users.update({

},{
    age:27
})

db.users.update({},{
    $set:{
        age:19,
        password:"hello"
    }
})

db.users.update({
    username:'hello'
},{
    $set:{
        age:39,
        password:"123123"
    }
})


db.users.updateOne({
    username:"bibi",
},{
    $set:{
        age:20,
        password:"qwe123"
    }
})

db.users.updateOne({
    username:"kiki",
},{
    $set:{
        age:30,
        password:"abc123"
    }
})


db.users.updateMany({

},{
    $set:{
        sex:"1"
    }
})

db.users.updateMany({
    username:"kity"
},{
    $set:{
        age:27
    }
})


db.users.update({
    username:"todo"
},{
    $set:{
        age:28,
        password:"todo"
    }
},true)
// true 如果查不到就直接插入数据  
db.users.update({
    
},{
    $set:{
        password:"000qwe"
    }
},false,true )
// true 这个 true 表示 可以修改多个数据  updateMany 



// 删除   deleteOne deleteMany remove

db.users.deleteOne({
    username:"bibi"
})

db.users.deleteMany({
    username:'todo'
})

db.users.remove({})

db.users.drop()   // 删除表  

db.users.updateMany({
    username:"happy"
},{
    $set:{
        age:19,
        password:"abc123"
    }
})
db.users.find()
db.users.find({username:"bibi"})
db.users.find({username:"happy"},{username:1,age:1})

db.users.find({username:"happy"},{username:1,age:1,_id:0})

db.users.find({username:"happy"},{_id:0})

db.users.find({username:"happy"},{_id:0,age:0})

db.users.find({username:"happy"},{_id:0,age:1})

db.users.distinct("age")
db.users.distinct("username")

// $gt   great then > 
// $gte  great then equal >=
// $lt   less then  <
// $lte  less then equal <= 

db.users.find({
    age:{
        $gt:20
    }
},{
    _id:0
})


db.users.find({
    age:{
        $lt:20
    }
},{
    _id:0
})

db.users.find({
    age:{
        $gte:20,
        $lte:30
    }
},{
    _id:0
})

// 模糊查询
db.users.find({username:/kiki/},{_id:0})

db.users.find({username:/^ki/},{_id:0})

db.users.find({username:/h/},{_id:0})

db.users.find({username:/a$/},{_id:0})

db.users.find({username:/^huahua$/},{_id:0})

db.users.find({username:new RegExp('a')},{_id:0})


// 电影
db.movies.find({},{_id:0,title:1})
db.movies.find({},{_id:0,title:1,year:1})
db.movies.find({},{title:1,_id:0,genres:1})
db.movies.find({},{title:1,_id:0,"rating.average":1})
db.movies.find({},{title:1,_id:0,"rating.min":1})
db.movies.find({year:{$gt:"1994",$lt:"2017"}},{title:1,year:1,_id:0})

db.movies.find({year:"1994"},{title:1,year:1,_id:0,genres:1});

db.movies.find({"rating.average":{$gte:9.3}},{year:1,title:1,_id:0,'rating.average':1})

db.movies.find({"rating.average":{$gte:9.3},year:"1994"},{year:1,title:1,_id:0,'rating.average':1})

db.movies.find({},{year:1,title:1,"directors.name":1,_id:0});

db.movies.find({},{_id:0,title:1,year:1}).sort({year:1})  // 升序
db.movies.find({},{_id:0,title:1,year:1}).sort({year:-1}) // 降序 
db.movies.find({},{_id:0,title:1,year:1,'rating.average':1}).sort({'rating.average':1}) 
db.movies.find({},{_id:0,title:1,year:1,'rating.average':1}).sort({'rating.average':-1}) 

db.movies.find({},{_id:0,title:1,year:1,'rating.average':1}).sort({year:1,'rating.average':-1}) 

db.movies.find({},{year:1,title:1,_id:0}).limit(10);

db.movies.find({},{year:1,title:1,_id:0}).skip(5);
db.movies.find({},{year:1,title:1,_id:0}).skip(5).limit(10);
db.movies.find({},{year:1,title:1,_id:0}).limit(10).skip(5);

db.movies.find({},{year:1,title:1,_id:0}).size();
db.movies.find({},{year:1,title:1,_id:0}).count();

db.movies.find({},{year:1,title:1,_id:0}).limit(10).count();

db.movies.find({},{year:1,title:1,_id:0}).limit(10).size();

db.movies.find({},{year:1,title:1,_id:0}).sort({year:-1}).limit(1)

db.movies.find({},{year:1,title:1,_id:0}).sort({year:1}).limit(1)

db.movies.find({},{year:1,title:1,_id:0,'rating.average':1}).sort({'rating.average':1}).limit(1)

db.movies.find({
    $or:[
        {year:'1994'},
        {
            'rating.average':{$gt:9.3}
        }
    ]
},{
    _id:0,
    title:1,
    year:1,
    'rating.average':1
})


db.movies.find({
    year:'1994',
    'rating.average':9.4
},{
    _id:0,
    title:1,
    year:1,
    'rating.average':1
})

db.movies.findOne({},{_id:0,title:1,year:1})

db.movies.find({},{_id:0,title:1,year:1}).limit(1)

db.movies.find({year:{$exists:true}},{year:1,title:1})   // 判断某个字段是否存在  

db.movies.find({year123:{$exists:true}},{year:1,title:1})

// $inc
db.users.update({
    username:"happy"
},{
    $inc:{
        age:12
    }
})

// $in  $nin 
db.movies.find({genres:{
    $in:['犯罪']
}},{
    title:1,
    year:1,
    genres:1,
    _id:0
})

db.movies.find({genres:{
    $in:['犯罪','动作','爱情']
}},{
    title:1,
    year:1,
    genres:1,
    _id:0
})



db.movies.find({genres:{
    $nin:['犯罪','剧情','爱情']
}},{
    title:1,
    year:1,
    genres:1,
    _id:0
})
